import Vue from 'vue'
import VueRouter from 'vue-router'

//路由
import login from '@/views/login'
import Home from '@/views/Home'
import Welcome from '@/views/Welcome'
import Managemusic from '@/views/Managemusic'
import Adminlikes from '@/views/Adminlikes'
import Userservice from '@/views/Userservice'
import Allorders from '@/views/Allorders'

Vue.use(VueRouter)


const routes = [
  //访问/路径的时候，重新定位到/admin
  {
    path:'/',
    redirect:'/admin'
  },
  {
    path:'/admin',
    name:'admin',
    component:Home,
    children:[
      {
        path:'/admin',
        redirect:'/admin/welcome'
      },
      {
        path:'welcome',
        name:'welcome',
        component:Welcome
      },
      {
        path:'managemusic',
        name:'managemusic',
        component:Managemusic
      },
      {
        path:'userservice',
        name:'userservice',
        component:Userservice
      },
      {
        path:'allorders',
        name:'allorders',
        component:Allorders
      },
      {
        path:'adminlikes',
        name:'adminlikes',
        component:Adminlikes
      }
    ]
  },
  {
    path:"/login",
    name:"login",
    component:login,
    meta:{
      title:"登陆界面"
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


//路由守卫
//前置路由守卫
router.beforeEach((to,from,next)=>{
  //1、获取token
  const adminToken=localStorage.getItem('adminToken')
  console.log('to=>',to);
  // console.log(object);
  //登陆页面不需要token验证
  if(to.path=='/login'){
    console.log('login');
    if(adminToken){
      next('/')   //进入首页
    }else{
      next()
    }
  }else{
    console.log('非/login');
    if(adminToken){
      next()
    }else{
      next('/login')
    }
  }
})
export default router
